package com.sumainfor.pc.dev.service.impl;


import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.dev.dao.RaidersHousingMapper;
import com.sumainfor.pc.dev.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.dev.service.RaidersHousingService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 租房攻略-房源中间表
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-09 19:59
 **/
@Service
public class RaidersHousingServiceImpl implements RaidersHousingService {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(RaidersHousingServiceImpl.class);


    @Autowired
    RaidersHousingMapper raidersHousingMapper;

    @Autowired
    ZpcMktFeign zpcMktFeign;

   /**
   * <p>
   * 获取租房攻略-房源中间表
   * </p>
   *
   * @author: zhoucong
   * @since:
   */
   @Override
    public JsonResult getRaidersHousing(Map<String,Object> params){
       List<String> HouInfoIdList = new ArrayList<>();
       List<Map<String,Object>> getRaidersHousing = raidersHousingMapper.getRaidersHousing(params);
       for (Map<String,Object> HouInfoIdMap:getRaidersHousing) {
           if (HouInfoIdMap.get("HouInfoId") !=null){
               HouInfoIdList.add(MapUtils.getString(HouInfoIdMap,"HouInfoId"));//添加房源详表编号
           }
       }
       JsonResult zpcResult = new JsonResult();
       if(HouInfoIdList.size()>0){
           params.put("HouInfoIdList",HouInfoIdList);
            zpcResult =zpcMktFeign.selectHousingHouInfoId(params);
       }
       return zpcResult;
   }

   /**
   * <p>
   * 增加租房攻略-房源中间表
   * </p>
   *
   * @author: zhoucong
   * @since:
   */
   @Override
    public JsonResult insertRadHou(Map<String,Object> params){
       JsonResult result = new JsonResult();
       /**
        * 加锁操作
        */
       lock.lock();
       try {
           log.info("增加操作");
              params.put("term", "7");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
              params.put("fnc",
                      this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
               ParamsCommon.commonInsertParms(params);
               Integer cout = raidersHousingMapper.insertRaidersHousing(params);
               if(cout<=0){
                   result.putFailed("创建失败!");
               }else{
                   result.putSuccess("创建成功!");
               }
       } catch (Exception e) {
           e.printStackTrace();
           TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
           result.putFailed("服务器繁忙...");
       } finally {
           lock.unlock();
       }
       return result;
   }

   /**
   * <p>
   * 删除租房攻略-房源中间表
   * </p>
   *
   * @author: zhoucong
   * @since:
   */
   @Override
   public JsonResult deleteRaidersHousing(Map<String,Object> params){
       JsonResult result = new JsonResult();

       /**
        * 加锁操作
        */
       lock.lock();
       try {
           log.info("删除操作");
               if(raidersHousingMapper.deleteRaidersHousing(params)<=0){
                   result.putFailed("删除失败!");
               }else{
                   result.putSuccess("删除成功!");
               }
       } catch (Exception e) {
           e.printStackTrace();
           TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
           result.putFailed("服务器繁忙...");
       } finally {
           lock.unlock();
       }
       return result;
   }

}
